package com.xxl.job.admin.core.util;

import java.io.*;

public class VipUtil {

    private static FileReader fr = null;
    private static BufferedReader bf = null;
    private static FileWriter fw = null;
    private static BufferedWriter bw = null;
    private static File tempFile_1 =null;
    private static File tempFile_2 =null;

    /**
     * 文件复制
     * @param srcFile       源文件路径
     * @param targetFile    目标文件路径
     * @throws Exception    异常信息
     */
    public static void copy(String srcFile,String targetFile) throws IOException{

        //command process


        //如果目标路径的目录不存在，创建它
        //String tempStr = targetFile.substring(0, targetFile.lastIndexOf("\\")+1);
        //System.out.println(tempStr);
        tempFile_2 = new File(targetFile);

        if(!tempFile_2.exists()){
            tempFile_2.mkdirs();
        }

        //初始化流
        fr = new FileReader(srcFile);
        bf = new BufferedReader(fr);
        fw = new FileWriter(targetFile);
        bw = new BufferedWriter(fw);

        tempFile_1 = new File(targetFile);
        //读取第一行
        String str =  bf.readLine();

        //判断文件有没有读取完
        while(str!=null){
            if(str.indexOf("priority")!=-1){
                bw.write("    priority 102");
                bw.newLine();
                bw.flush();
                str = bf.readLine();
            }
            else{
                bw.write(str);
                bw.newLine();
                bw.flush();
                str = bf.readLine();
            }

        }


        if(tempFile_1.exists()){
            System.out.println("文件复制成功");
            Process process =Runtime.getRuntime().exec("cp keepalived.conf keepalived.conf.sample");
        }
    }

    /**
     * 关闭流
     */
    public static void close() throws IOException{
        bw.flush();
        bw.close();
        fw.close();
        bf.close();
        fr.close();
    }
}
